<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2020 http://121.43.104.7 All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang@fistforward.com
// +----------------------------------------------------------------------

/**
 * 活动ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang@fistforward.com
 */
quanli_require('module/base/system/activityConfig.class.php');

class ajax_activity {

    private $config_array = array();

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->searchList();
                    break;

                case 2:
                    $this->edit();
                    break;

                case 6:
                    $this->userPersonnel();
                    break;
                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit'))) {
                        returnJsonData('参数错误');
                    }
                    if ($_REQUEST['act'] == 'edit') {

                        $this->editConfig();//新增、修改

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteConfig();//删除

                    }

                    break;
            }
        } else {

            $this->toReturn('参数错误');
        }
    }

    private function searchList(){

        global $G_PGLOB;

        //实例用户类
        $activityConfig = new activityConfig($G_PGLOB->DBCA);
        if( isset($_REQUEST['search_departmentID']) ){
            $_REQUEST['departmentID'] =  $_REQUEST['search_departmentID'];
        }

        //查询用户信息
        $list = $activityConfig->searchActivityInfo($_REQUEST);

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ( $list ) {

            $edit_permission = $G_PGLOB->has_permission(32);

            foreach ($list as &$data) {


                if ($edit_permission) {

                    $operate = '<a href="javascript: void(0);" onclick="config.edit(\'' . $data['config_id'] . '\');">修改</a>';
                    $result_array['rows'][] = array(
                        'id' => $data['config_id'],
                        'cell' => array(
                            $operate,
                            $data['config_id'],
                            $data['config_code'],
                            $data['config_key'],
                            $data['config_value'],
                            $data['config_remark']
                        )
                    );
                } else {

                    $result_array['rows'][] = array(
                        'id' => $data['config_id'],
                        'cell' => array(
                            $data['config_id'],
                            $data['config_code'],
                            $data['config_key'],
                            $data['config_value'],
                            $data['config_remark']
                        )
                    );
                }
            }
        }

        echo json_encode($result_array);
    }

    private function edit(){

        global $G_PGLOB;

        //实例用户类
        $config = new activityConfig($G_PGLOB->DBCA);

        /**
         * =============================
         * 查询信息
         * =============================
         */
        if (isset($_REQUEST['config_id']) && !empty($_REQUEST['config_id'])) {

            //查询信息
            $arg_array = array(
                'config_id' => ceil($_REQUEST['config_id'])
            );

            $row = $config->searchActivityInfo($arg_array);

            if (!$row) {
                $this->toError('记录没有找到');
            }

            $data_array = $row[0];

            returnJsonData('ok', 200, $data_array);

        } else {
            returnJsonData('ok', 404);
        }
    }

    /**
     * 保存/修改
     */
    private function editConfig() {

        global $G_PGLOB;

        //实例用户类
        $sysConfig = new activityConfig($G_PGLOB->DBCA);

        if(!get_magic_quotes_gpc()){
            addslashes($_POST);
        }


        //新增或修改用户信息
        $arg_array['creator'] = $G_PGLOB->currentUserID;
        if(isset($_POST['uuid']) && !empty($_POST['uuid'])){
            $arg_array['uuid'] = trim($_POST['uuid']);
        }
        $arg_array['config_code'] = trim($_POST['config_code']);
        $arg_array['config_key'] = trim($_POST['config_key']);
        $arg_array['config_value'] = trim($_POST['config_value']);
        $arg_array['config_remark'] = trim($_POST['config_remark']);

        $result_array = $sysConfig->editConfig($arg_array);

        echo json_encode($result_array);

    }

    /**
     * 删除
     */
    private function deleteConfig(){

        if (!isset($_REQUEST['ids']) || !is_array($_REQUEST['ids'])) {
            $this->toReturn('参数错误');
        }

        global $G_PGLOB;

        //实例化用户类
        $config = new activityConfig($G_PGLOB->DBCA);

        $result_array = $config->deleteConfig($_REQUEST['ids']);

        echo json_encode($result_array);
    }
}




